<style type="text/css" title="currentStyle">
	@import "../media/css/demo_page.css";
	@import "../media/css/demo_table_jui.css";
	@import "../examples_support/themes/smoothness/jquery-ui-1.8.4.custom.css";
	@import "media/css/TableTools_JUI.css";
</style>
<script type="text/javascript" charset="utf-8" src="../media/js/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8" src="media/js/ZeroClipboard.js"></script>
<script type="text/javascript" charset="utf-8" src="media/js/TableTools.js"></script>
<script type="text/javascript" charset="utf-8">
	$(document).ready( function () {
		$('#example').dataTable( {
			"bJQueryUI": true,
			"sPaginationType": "full_numbers",
			"sDom": '<"H"Tfr>t<"F"ip>',
			"oTableTools": {
				"aButtons": [
					"copy", "csv", "xls", "pdf", "print",
					{
						"sExtends":    "collection",
						"sButtonText": "Save",
						"aButtons":    [ "csv", "xls", "pdf" ]
					}
				]
			}
		} );
	} );
</script>
<?php
//print_r($_POST);
if(isset($_POST[txtDelContId]) && $_POST[txtDelContId]!="")
{
	//$data['di_assignedDriverId'] = 0;
	//if($db->query_update("tb_deviceinfo", $data , "di_id=".$deviceAllId[$i]))	
	$sql = "DELETE FROM tb_client_driver_info WHERE tcdi_id =".$_POST[txtDelContId];
	$rows = $db->query($sql);
}
if(isset($_POST[txtContId]) && $_POST[txtContId] !='' && $_POST[txtDeviceId] == '' && $_POST[txtAllDeviceId] =='')
{
	$sql = "SELECT * FROM tb_client_driver_info WHERE tcdi_id =".$_POST[txtContId];
	$rows = $db->query($sql);
	if($db->affected_rows > 0)
	{
		$deviceRecord = $db->fetch_array($rows);
	}
}
if(isset($_POST[txtContId]) && $_POST[txtContId] !='' && $_POST[txtDeviceId] == '' && $_POST[txtAllDeviceId]!='')
{
	//exit;
	header("location:?ch=status&au=24&msg=0");
	exit;
}
if(isset($_POST[txtContId]) && $_POST[txtContId] !='' && $_POST[txtDeviceId] != '' && $_POST[txtAllDeviceId]!='')
{
	$deviceAllId = explode(",",$_POST[txtAllDeviceId]);
	
	for($i=0;$i<count($deviceAllId)-1;$i++)
	{
		//echo "<br>".$_POST[txtDeviceId]."=".$deviceAllId[$i];
		if($_POST[txtDeviceId] == $deviceAllId[$i])
		{
			$data['di_assignedDriverId'] = $_POST[txtContId];	
		}
		else
		{
			$data['di_assignedDriverId'] = 0;
		}
		$db->query_update("tb_deviceinfo", $data , "di_id=".$deviceAllId[$i]);
	}
	
	header("location:?ch=status&au=26&msg=1");
	exit;
	
}

?>
<script type="text/javascript" language="javascript">
function mobileNoValid(id)
{
	objMobileNo = document.getElementById(id);
	if(document.getElementById(id).value.length != 10 || document.getElementById(id).value.search(/[^0-9\-()+]/g) != -1 )
	{
		//alert('Please enter valid 10 digit mobile number');
		//objMobileNo.focus();
		//objMobileNo.value="";
		return false;
	}
	else
	return true; 
	
}
var ajax1=new sack();

function validContForm(type)
{
var f = 0;	
	if(document.getElementById('txtContName').value == "" )
	{
		alert("Enter Name"); 
		document.getElementById('txtContName').focus();
		f = 1;
		return false;
	}
	else f =0;
	alrtType = 'Mobile';
	if(document.getElementById('txtMobileEmail').value == "")
	{
		alert("Enter Mobile Number"); 
		document.getElementById('txtMobileEmail').focus();
		f = 1;
		return false;
	}
	else
	{
		if(!mobileNoValid('txtMobileEmail'))
		{
			alert('Please enter valid 10 digit mobile number');
			document.getElementById('txtMobileEmail').focus();
			f = 1;
			return false;
		}
		else f =0;
	}
	//alert(f);
	if(f==0)
	{
		//qry = "select * from tb_userinfo where ui_username = '"+val+"'";
		document.getElementById('cmdUpdateCont').value = "wait...";
		document.getElementById('cmdUpdateCont').disabled = true;
		if(type == 'add')
			ajax1.requestFile = 'ajax_server.php?drivertype='+type+'&name='+document.getElementById('txtContName').value+'&src='+document.getElementById('txtMobileEmail').value+'&license='+document.getElementById('txtDriveLicense').value+'&dob='+document.getElementById('txtDOB').value;
		else
			ajax1.requestFile = 'ajax_server.php?drivertype='+type+'&name='+document.getElementById('txtContName').value+'&src='+document.getElementById('txtMobileEmail').value+'&license='+document.getElementById('txtDriveLicense').value+'&dob='+document.getElementById('txtDOB').value+'&cid='+document.getElementById('txtContId').value;
				
		//alert(ajax1.requestFile+" "+type);
		ajax1.onCompletion = function(){addContact()};
		ajax1.runAJAX();
	}
	
}
function addContact()
{
	if(ajax1.response ==1 )
	{
		alert("Data Added Successfully");
		window.location.href = '?ch=viewDriver'; 
	}
	else  if(ajax1.response ==0 )
	{
		alert("Data not Added");
		window.location.href = '?ch=viewDriver'; 
	}
	else  if(ajax1.response == 2 )
	{
		alert("Data already Exist");
		window.location.href = '?ch=viewDriver'; 
	}
	else  if(ajax1.response == 3 )
	{
		alert("Data Updated Successfully");
		window.location.href = '?ch=viewDriver'; 
	}
}
function funEditUser(drid,did,aid,act)
{
	document.frmSubmit.txtContId.value = drid;
	document.frmSubmit.txtDeviceId.value = did;
	document.frmSubmit.txtAllDeviceId.value = aid;
	document.frmSubmit.action = act;
	document.frmSubmit.submit();
}

function deleteUser(did,act)
{
  if (confirm("Are you sure you want to delete")) 
  {
	document.frmDelSubmit.txtDelContId.value = did;
	document.frmDelSubmit.action = act;
	document.frmDelSubmit.submit();
  }
}
function dispDeviceID(val)
{
	document.getElementById('selDeviceId').value = val;
}
function sendCSVData()
{
//alert(c1);
document.frmTripData.submit();
//	window.location.href='export.php?csvData='+c1+'&frdate='+c2+'&frtime='+c3+'&totime='+c4+'&vehino='+c5;
	//document.write('export.php?csvData='+c1+'&frdate='+c2+'&frtime='+c3+'&totime='+c4+'&vehino='+c5);
}
$(function() {
	$( "#txtDOB" ).datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: "dd-mm-yy"
	});
});
</script>
<form id="frmAddPolicy" name="frmAddPolicy" method="post" action="?ch=Driver"> 
<input type="hidden" name="txtContId" id="txtContId" value="<?php echo $_POST[txtContId];?>" />
<input type="hidden" name="txtClientId" id="txtClientId" value="<?php echo $_POST[txtClientId];?>" />
<table class="gridform_final">
<tr><th colspan="2">Add Driver</th></tr>
  <tr>
    <td width="20%" align="right">Driver Name</td>
    <td width="30%" align="left">
        <input type="text" name="txtContName" id="txtContName" tabindex="1" value="<?php echo $deviceRecord[tcdi_driverName];?>" />
    </td>
  </tr>              
  
  <tr>
    <td align="right">Mobile No</td>
    <td align="left">
        <input type="text" name="txtMobileEmail" id="txtMobileEmail" tabindex="2" value="<?php echo $deviceRecord[tcdi_mobile];?>"/>
  </tr>
  <tr>
    <td width="20%" align="right">Driving License No</td>
    <td width="30%" align="left">
        <input type="text" name="txtDriveLicense" id="txtDriveLicense" tabindex="3" value="<?php echo $deviceRecord[tcdi_licenseNo];?>" />
    </td>
  </tr>              
  
  <tr>
    <td align="right">Date of Birth</td>
    <td align="left">
    <input type="text" name="txtDOB" id="txtDOB" tabindex="4" value="<?php if(isset($deviceRecord[dob])) echo $deviceRecord[dob]; else echo date("d-m-Y"); ?>" maxlength="10" />
    <span>Example : 30-12-1985 (dd-mm-yyyy) </span>
    </td>
  </tr>
  <tr>
    <td style="text-align:right">
  
    <?php if(isset($_POST[txtContId]) && $_POST[txtContId]!='')  { ?>
	  <input type="button" name="cmdUpdateCont" id="cmdUpdateCont" value="Update" tabindex="5" class="save_btn" onclick="validContForm('update');"/> 
	<?php } else { ?>
      <input type="button" name="cmdUpdateCont" id="cmdUpdateCont" value="Add" class="save_btn" tabindex="5" onclick="validContForm('add');" />    
    <?php } ?>
    </td>
    <td align="left">
      <input type="button" name="map_cancel_btn" id="map_cancel_btn" value="Cancel" class="save_btn" onclick="location.href='index.php?ch=viewDriver';" tabindex="6" /> 
    </td>
  </tr>  
</table>
</form>
<br />
<?php
$selInsur = "SELECT * FROM tb_client_driver_info WHERE tcdi_clientId = ".$_SESSION[clientID];
$resInsur = $db->query($selInsur);
?>
<table class="gridform_final" id="example">
<thead>
    <tr>
		<th width="5%">#</th>
        <th width="20%">Driver Name</th>
        <th width="15%">Mobile</th>
        <th width="15%">License</th>
        <th width="10%">DOB</th>
        <th width="15%">Device</th>
        <th width="10%">Edit</th>
        <th width="10%">Delete</th>
   </tr>
</thead>
<tbody>   
	 <?php 
	 if($db->affected_rows)
	 {
	 $i = 1;
    while($fetInsur = $db->fetch_array($resInsur)) 
    { 
		$dev= $util->getDeviceInfoByDriverId($fetInsur[tcdi_id]);
		$device = "";
		$c = 0;
		while($fetDeviceId = $db->fetch_array($dev))
		{
			if($fetDeviceId[di_deviceName])
				$device .= $fetDeviceId[di_deviceName];
			else
				$device .= $fetDeviceId[di_deviceId];
				
			//$device .= "<tr><th><input type='text' name='txtAssId".$c."' id='txtAssId".$c."' value=".$fetDeviceId[di_id]." /></th></tr>";
			$c++;
		}
		
		if($_POST[txtContId] == $fetInsur[tcdi_id]) 
		{
			
			$getUserDevice = "SELECT * FROM tb_deviceinfo,tb_client_subscription WHERE tcs_isActive = 1 AND tcs_deviceId = di_id AND di_status = 1 AND (di_assignedDriverId = 0 or di_assignedDriverId = $fetInsur[tcdi_id]) AND di_clientId = ".$_SESSION[clientID]." ORDER BY di_deviceName,di_deviceId ASC";
			$rowUserDevice = $db->query($getUserDevice);
			if($db->affected_rows > 0)
			{
				$device = '<select name="selUserDevice" style="position:absolute;" id="selUserDevice" onchange="dispDeviceID(this.value);">';
				$device .= '<option value="0">Choose Device</option>';
				while ($fetUserDevice = $db->fetch_array($rowUserDevice)) 
				{			
				$renewDate = date("d-m-Y",strtotime("-1 days ".$fetUserDevice[tcs_renewalDateTo]));
				//echo $date_offline." <= ".date("d-m-Y",strtotime($renewDate))."<br>";
				if(strtotime(date('d-m-Y')) <= strtotime($renewDate))
				{
						if($fetUserDevice[di_deviceName])
							$devName = $fetUserDevice[di_deviceName];
						else
							$devName = $fetUserDevice[di_deviceId];
						
						$allIds .=$fetUserDevice[di_id].",";	
						
						if($fetInsur[tcdi_id] == $fetUserDevice[di_assignedDriverId])
						{
							$sel = 'selected="selected"';
							$ids .= $fetUserDevice[di_id];
							
						}
						else
						{
							$sel ='';
						}
						$device .= '<option '.$sel.' value="'.$fetUserDevice[di_id].'">'.$devName.'</option>';
						
						}
					}
					$device .= '</select><br><input type="hidden" name="selDeviceId" id="selDeviceId" value="'.$ids.'" /><br><input type="hidden" name="allDeviceId" id="allDeviceId" value="'.$allIds.'" />';	
					
				
			}
		}
		if(!$device)
		{
			$device = 'Not Assigned';
		}
	
    ?>
   <tr>
   	<td><?php echo $i;?>
    <td><?php echo ucfirst($fetInsur[tcdi_driverName]);?></td>
    <td><?php echo $fetInsur[tcdi_mobile];?></td>
    <td><?php echo $fetInsur[tcdi_licenseNo];?></td>
    <td><?php if($fetInsur[tcdi_dob] != "1970-01-01") echo date("d-m-Y",strtotime($fetInsur[tcdi_dob]));?></td>
    <td>
        <?php if($_POST[txtContId] == $fetInsur[tcdi_id])   { ?>
		<a href="#" onclick="funEditUser('<?php echo $_POST[txtContId];?>',document.getElementById('selDeviceId').value,document.getElementById('allDeviceId').value,'?ch=viewDriver')">Assign</a> | <a href="#" onclick="location.href='?ch=viewDriver';">Cancel</a><br /><?php echo $device;?>
        <?php } else { ?><?php echo $device;?>
        <a href="#" onclick="funEditUser('<?php echo $fetInsur[tcdi_id];?>','','','?ch=viewDriver')" onmouseover="document.getElementById('dev<?php echo $i;?>').style.display='block';" onmouseout="document.getElementById('dev<?php echo $i;?>').style.display='none';">Edit<span style="display:none; position:absolute" id="dev<?php echo $i;?>"></span></a> 
        <?php } ?>
        </td>
    <td><img src="../../images/edit.gif" style="cursor:pointer;" width="45" height="16" border="0" onclick="funEditUser('<?php echo $fetInsur[tcdi_id];?>','','','?ch=viewDriver')" /></td>
	<td><img src="../../images/delete.gif" style="cursor:pointer;" width="45" height="16" border="0" onclick="deleteUser('<?php echo $fetInsur[tcdi_id];?>','?ch=viewDriver')" /></td>
  </tr>
  <?php
    $device = "";
	$assignIds.=$record[ui_id]."#";
	$i++;
	}
	 }
?>
</tbody>  
</table>

<form name="frmSubmit" id="frmSubmit" method="post">
    <input type="hidden" name="txtContId" id="txtContId" />
    <input type="hidden" name="txtDeviceId" id="txtDeviceId" />
    <input type="hidden" name="txtAllDeviceId" id="txtAllDeviceId" />
</form>
<form name="frmDelSubmit" id="frmDelSubmit" method="post">
<input type="hidden" name="txtDelContId" id="txtDelContId" />
</form>